﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Diplom.Utilities;
using Diplom.NumericArrays;
using Diplom.Wavelets;
using Diplom.Graphics.D2D;
using Diplom.Graphics.D3D;

namespace Diplom.Forms
{
    public partial class FilterDesigner : Form
    {
        Numeric1DArray numeric1Start = null;
        Numeric1DArray numeric1Result = null;

        public FilterDesigner()
        {
            InitializeComponent();
        }

        private void upDownOrder_ValueChanged(object sender, EventArgs e)
        {
            buttonApply_Click(sender, e);
        }

        private void upDownLevel_ValueChanged(object sender, EventArgs e)
        {
            buttonApply_Click(sender, e);
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt,*.dat*,*.in,*.out)|*.txt;*.dat;*.in;*.out";
            openFileDialog.Multiselect = false;
            openFileDialog.InitialDirectory = System.Environment.CurrentDirectory;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(openFileDialog.FileName))
                {
                    return;
                }
                numeric1Start = new Numeric1DArray();
                ArrayGenerator.Generate(openFileDialog.FileName,
                    ref numeric1Start);

                this.Refresh();
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            if (numeric1Start != null)
            {
                try
                {
                    numeric1Result = new Numeric1DArray(numeric1Start);
                    Filter.Denoise(ref numeric1Result, (int)upDownOrder.Value, (int)upDownLevel.Value);
                }
                catch
                {
                }

                this.Refresh();
            }
        }

        public Numeric1DArray Array
        {
            get { return numeric1Result; }
            set
            {
                numeric1Start = new Numeric1DArray(value);

                numeric1Result = null;
            }
        }

        private void FilterDesigner_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics graphics;
            if (numeric1Start != null && numeric1Start.Length > 0)
            {
                graphics = System.Drawing.Graphics.FromImage(pictureStart.Image);
                graphics.Clear(Color.White);
                DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red);
                D2DVisualizer.DrawGraphic(ref graphics,
                    ref numeric1Start,
                    new RectangleF(40, 20,
                        pictureStart.Width - 70, pictureStart.Height - 60), style);
                D2DVisualizer.DrawSignature(ref graphics, ref numeric1Start, new RectangleF(40, 20,
                        pictureStart.Width - 70, pictureStart.Height - 60), style, new Size(10, 10));
            }
            if (numeric1Result != null && numeric1Result.Length > 0)
            {
                graphics = System.Drawing.Graphics.FromImage(pictureResult.Image);
                graphics.Clear(Color.White);
                DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red);
                D2DVisualizer.DrawGraphic(ref graphics,
                    ref numeric1Result,
                    new RectangleF(40, 20,
                        pictureResult.Width - 70, pictureResult.Height - 60), style);
                D2DVisualizer.DrawSignature(ref graphics, ref numeric1Result, 
                    new RectangleF(40, 20,
                        pictureResult.Width - 70, pictureResult.Height - 60), style, new Size(10, 10));
            }
        }

        private void FilterDesigner_Load(object sender, EventArgs e)
        {
            pictureStart.Image = new Bitmap(pictureStart.Width, pictureStart.Height);
            pictureResult.Image = new Bitmap(pictureResult.Width, pictureResult.Height);
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (numeric1Result == null)            
            {
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.txt,*.dat*,*.in,*.out)|*.txt;*.dat;*.in;*.out";
            saveFileDialog.InitialDirectory = System.Environment.CurrentDirectory;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(saveFileDialog.FileName))
                {
                    return;
                }
                ArrayGenerator.SaveArray(ref numeric1Result, saveFileDialog.FileName);
            }
        }
    }
}
